package com.yq.cp.controller.yw;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.data.dto.YwZbglJjbDto;
import com.yq.cp.entity.yw.YwZbglJjb;
import com.yq.cp.entity.yw.vo.YwZbglJjbVo;
import com.yq.cp.service.yw.YwZbglJjbService;
import com.yq.cp.utils.ExcelUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运维-值班管理-交接班 前端控制器
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/yw-zbgl-jjb")
public class YwZbglJjbController {

    @Resource
    YwZbglJjbService ywZbglJjbService;

    @ResponseBody
    @PostMapping("add")
    public ApiResult add(@RequestBody YwZbglJjb ywZbglJjb) {
        ywZbglJjbService.add(ywZbglJjb);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("sc")
    public ApiResult sc(@RequestBody YwZbglJjb ywZbglJjb) {
        ywZbglJjbService.sc(ywZbglJjb);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("jb")
    public ApiResult jb(@RequestBody YwZbglJjb ywZbglJjb) {
        ywZbglJjbService.jb(ywZbglJjb);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("csh")
    public ApiResult csh(@RequestBody YwZbglJjb ywZbglJjb) {
        ywZbglJjbService.csh(ywZbglJjb.getSszdId());
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("page")
    public ApiResult page(@RequestBody YwZbglJjbVo vo) {
        IPage<YwZbglJjb> page = ywZbglJjbService.page(vo);
        return ApiResult.ok().data(page);
    }

    @ResponseBody
    @PostMapping("page/daochu")
    public ApiResult pageDaochu(@RequestBody YwZbglJjbVo vo) {
        List<YwZbglJjbDto> daochu = ywZbglJjbService.daochu(vo);

        ExcelUtil.doWrite("交接班信息","交接班信息",YwZbglJjbDto.class,daochu);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("cxxyczbxx")
    public ApiResult cxxyczbxx(@RequestBody YwZbglJjb vo) {
        Map<String, Object> cxxyczbxx = ywZbglJjbService.cxxyczbxx(vo);
        return ApiResult.ok().data(cxxyczbxx);
    }

    @ResponseBody
    @PostMapping("get")
    public ApiResult get(@RequestBody YwZbglJjb vo) {
        YwZbglJjb ywZbglJjb = ywZbglJjbService.get(vo.getId());
        return ApiResult.ok().data(ywZbglJjb);
    }

    @PostMapping("/get/daochu")
    public void getDaochu(@RequestBody @Valid YwZbglJjb vo, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {

        YwZbglJjb ywZbglJjb = ywZbglJjbService.get(vo.getId());
        // 防止日志记录获取session异常
        request.getSession();
        // 设置编码格式
        response.setContentType("application/pdf;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("交接班详情", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");
        ywZbglJjbService.getDaochu(ywZbglJjb, response);
    }
}

